﻿using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wlog.Model;

namespace wlog.Shell.Storage.Table
{
    public class TableInsertCommand : TableStorageCommand
    {
        public override void Setup(string[] args)
        {
            base.Setup(args);
            base.CreateTask(() => { RunCommand(); });
        }
        public OutputChannel RunCommand()
        {
            try
            {
                var tableAPI = GetTableAccount();
                var table = tableAPI.GetTableReference(objectName);
                bool success = table.CreateIfNotExists();
                var log = new LogLevel();
                log.Level = (CommandArgs.Length > 3) ? CommandArgs[3] : "SaticLevel";
                log.Description = (CommandArgs.Length > 4) ? CommandArgs[4] : "Static Description";

                TableOperation insertOperation = TableOperation.Insert(log);
                var ctx = tableAPI.GetTableServiceContext();
                ctx.AddObject("LogLevel", log);
                ctx.SaveChanges();
            }
            catch (Exception azureEx)
            {
                Results.WriteLine("Error creating table {0} ", azureEx.Message);
            }
            return Results;
        }
    }
}

